#pragma once

#include"nocopy.hpp"
#include<sys/epoll.h>
#include"Log.hpp"
#include"string.h"
class Epoller:public Nocopy
{
    static const int size = 128;
public:
    Epoller()
    {
        _epfd = epoll_create(size);
        if(_epfd == -1)
        {
            lg(Error, "epoll create error:%s",strerror(errno));
        }
        else
        {
            lg(Info, "epoll create succsee, epfd:%d", _epfd);
        }
    }

    int  EpollWait(struct epoll_event revents[], int num)
    {
        int n = epoll_wait(_epfd, revents, num, _timeout);
        return n;
    }

    int EpllerUpdate(int op, int sock, uint32_t event)
    {
        int n = 0;
        if(op == EPOLL_CTL_DEL)
        {
            n = epoll_ctl(_epfd, op, sock, nullptr);
        }
        else
        {
            struct epoll_event ev;
            ev.events = event;
            ev.data.fd = sock;
            //设置进内核
            n = epoll_ctl(_epfd, op, sock, &ev);
            if(n != 0)
            {
                lg(Error, "epoll_ctl error");
            }
        }
        return n;
    }
    ~Epoller()
    {
        if(_epfd >= 0) close(_epfd);
    }
private:
    int _epfd;
    int _timeout{3000};
};
